package cn.com.css.taxInspection.workflow.controller;

import cn.com.css.taxInspection.gt3.entity.*;
import cn.com.css.taxInspection.gt3.service.*;
import cn.com.css.taxInspection.workflow.vo.excel.JcAyxxJgRyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.Sheet;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.*;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itextpdf.io.IOException;

import cn.com.css.taxInspection.common.aop.annotation.LogAnnotation;
import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.common.result.DataResult;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.com.css.taxInspection.workflow.constants.WorkflowConstants;
import cn.com.css.taxInspection.workflow.entity.WorkflowDebugtaxEntity;
import cn.com.css.taxInspection.workflow.entity.WorkflowInspectionHearEntity;
import cn.com.css.taxInspection.workflow.service.WorkflowDebugtaxService;
import cn.com.css.taxInspection.workflow.service.WorkflowInspectionHearService;
import cn.com.css.taxInspection.workflow.vo.excel.JcAyxxExcel;
import cn.com.css.taxInspection.workflow.vo.excel.TaxorgcodeExcel;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;

/**
 * ${comments}
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2023-10-04 00:23:58
 */
@Controller
@RequestMapping("/workflow/inspect")
public class WorkflowInspectionController {

    private static final Logger log = LoggerFactory.getLogger(WorkflowInspectionController.class);
    @Autowired
    private HttpSessionService httpSessionService;

    @Autowired
    private WorkflowInspectionHearService workflowInspectionHearService;

    @Autowired
    private WorkflowDebugtaxService workflowDebugtaxService;

    @Autowired
    private JcAjxxService jcAjxxService;

    @Autowired
    private JcAyxxService jcAyxxService;

    @Autowired
    private JcAjtzZcajxxService jcAjtzZcajxxService;

    @Autowired
    private JcAjtzSlytzService jcAjtzSlytzService;

    @Autowired
    private DmGySwjgService dmGySwjgService;

    @Autowired
    private JcAjtzztbService jcAjtzztbService;

    @Autowired
    private JcAyxxDebugService jcAyxxDebugService;

    @Autowired
    private JcAyxxDebugJgryService jcAyxxDebugJgryService;

    @Autowired
    private DjNsrxxService djNsrxxService;

    @Autowired
    private DjNsrxxKzService djNsrxxKzService;

    @Value("${file.path}")
    private String filePath;
    @Value("${file.url}")
    private String fileUrl;

    @ApiOperation(value = "检查人环节-个人待办")
    @LogAnnotation(title = "检查人环节", action = "个人待办")
    @PostMapping("/listwait11")
    @RequiresPermissions("workflow:inspect:wait11")
    @ResponseBody
    @DS("oracle")
    public DataResult listwait11(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {
        List<JcAjxxEntity> iPage = jcAjxxService.pageInfo11(workflowInspectionHear);
        return DataResult.success(iPage);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "检查人环节-个人已办")
    @LogAnnotation(title = "检查人环节", action = "个人已办")
    @PostMapping("/listthen11")
    @RequiresPermissions("workflow:inspect:then11")
    @ResponseBody
    @DS("oracle")
    public DataResult listthen11(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {
        String currentUserId = httpSessionService.getCurrentUserId();
        String currentDeptId = httpSessionService.getCurrentDeptId();
        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<WorkflowInspectionHearEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.like(WorkflowInspectionHearEntity::getInspectionMan11, currentUserId);
        queryWrapper.eq(WorkflowInspectionHearEntity::getInspectionTaxorg11, currentDeptId);
        IPage<WorkflowInspectionHearEntity> iPage = workflowInspectionHearService.page(page, queryWrapper);
        List<WorkflowInspectionHearEntity> records = iPage.getRecords();
        for (WorkflowInspectionHearEntity workflowInspectionHearEntity : records) {
            String relevance = workflowInspectionHearEntity.getRelevance();
            String stateCode = workflowInspectionHearEntity.getStateCode();
            String stateName = workflowInspectionHearEntity.getStateName();
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, workflowInspectionHearEntity);
            workflowInspectionHearEntity.setStateCode(stateCode);
            workflowInspectionHearEntity.setStateName(stateName);
        }
        return DataResult.success(iPage);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "检查科科长环节-个人待办")
    @LogAnnotation(title = "检查科科长环节", action = "个人待办")
    @PostMapping("/listwait12")
    @RequiresPermissions("workflow:inspect:wait12")
    @ResponseBody
    @DS("oracle")
    public DataResult listwait12(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {
        String currentUserId = httpSessionService.getCurrentUserId();
        String currentDeptId = httpSessionService.getCurrentDeptId();
        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<WorkflowInspectionHearEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.eq(WorkflowInspectionHearEntity::getInspectionMan12, currentUserId);
        queryWrapper.eq(WorkflowInspectionHearEntity::getInspectionTaxorg12, currentDeptId);
        queryWrapper.eq(WorkflowInspectionHearEntity::getStateCode, WorkflowConstants.INSPECT_12);
        IPage<WorkflowInspectionHearEntity> iPage = workflowInspectionHearService.page(page, queryWrapper);
        List<WorkflowInspectionHearEntity> records = iPage.getRecords();
        for (WorkflowInspectionHearEntity workflowInspectionHearEntity : records) {
            String relevance = workflowInspectionHearEntity.getRelevance();
            String stateCode = workflowInspectionHearEntity.getStateCode();
            String stateName = workflowInspectionHearEntity.getStateName();
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, workflowInspectionHearEntity);
            workflowInspectionHearEntity.setStateCode(stateCode);
            workflowInspectionHearEntity.setStateName(stateName);
        }
        return DataResult.success(iPage);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "检查科科长环节-个人已办")
    @LogAnnotation(title = "检查科科长环节", action = "个人已办")
    @PostMapping("/listthen12")
    @RequiresPermissions("workflow:inspect:then12")
    @ResponseBody
    @DS("oracle")
    public DataResult listthen12(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {
        String currentUserId = httpSessionService.getCurrentUserId();
        String currentDeptId = httpSessionService.getCurrentDeptId();
        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<WorkflowInspectionHearEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.eq(WorkflowInspectionHearEntity::getInspectionMan12, currentUserId);
        queryWrapper.eq(WorkflowInspectionHearEntity::getInspectionTaxorg12, currentDeptId);
        IPage<WorkflowInspectionHearEntity> iPage = workflowInspectionHearService.page(page, queryWrapper);
        List<WorkflowInspectionHearEntity> records = iPage.getRecords();
        for (WorkflowInspectionHearEntity workflowInspectionHearEntity : records) {
            String relevance = workflowInspectionHearEntity.getRelevance();
            String stateCode = workflowInspectionHearEntity.getStateCode();
            String stateName = workflowInspectionHearEntity.getStateName();
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, workflowInspectionHearEntity);
            workflowInspectionHearEntity.setStateCode(stateCode);
            workflowInspectionHearEntity.setStateName(stateName);
        }
        return DataResult.success(iPage);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "检查科主管局长环节-个人待办")
    @LogAnnotation(title = "检查科主管局长环节", action = "个人待办")
    @PostMapping("/listwait13")
    @RequiresPermissions("workflow:inspect:wait13")
    @ResponseBody
    @DS("oracle")
    public DataResult listwait13(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {
        String currentUserId = httpSessionService.getCurrentUserId();
        String currentDeptId = httpSessionService.getCurrentDeptId();
        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<WorkflowInspectionHearEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.eq(WorkflowInspectionHearEntity::getInspectionMan13, currentUserId);
        queryWrapper.eq(WorkflowInspectionHearEntity::getInspectionTaxorg13, currentDeptId);
        queryWrapper.eq(WorkflowInspectionHearEntity::getStateCode, WorkflowConstants.INSPECT_13);
        IPage<WorkflowInspectionHearEntity> iPage = workflowInspectionHearService.page(page, queryWrapper);
        List<WorkflowInspectionHearEntity> records = iPage.getRecords();
        for (WorkflowInspectionHearEntity workflowInspectionHearEntity : records) {
            String relevance = workflowInspectionHearEntity.getRelevance();
            String stateCode = workflowInspectionHearEntity.getStateCode();
            String stateName = workflowInspectionHearEntity.getStateName();
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, workflowInspectionHearEntity);
            workflowInspectionHearEntity.setStateCode(stateCode);
            workflowInspectionHearEntity.setStateName(stateName);
        }
        return DataResult.success(iPage);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "检查科主管局长环节-个人待办")
    @LogAnnotation(title = "检查科主管局长环节", action = "个人待办")
    @PostMapping("/listthen13")
    @RequiresPermissions("workflow:inspect:then13")
    @ResponseBody
    @DS("oracle")
    public DataResult listthen13(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {
        String currentUserId = httpSessionService.getCurrentUserId();
        String currentDeptId = httpSessionService.getCurrentDeptId();
        Page page = new Page(workflowInspectionHear.getPage(), workflowInspectionHear.getLimit());
        LambdaQueryWrapper<WorkflowInspectionHearEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowInspectionHearEntity::getInspectionMan13, currentUserId);
        queryWrapper.eq(WorkflowInspectionHearEntity::getInspectionTaxorg13, currentDeptId);
        // 查询条件示例
        IPage<WorkflowInspectionHearEntity> iPage = workflowInspectionHearService.page(page, queryWrapper);
        List<WorkflowInspectionHearEntity> records = iPage.getRecords();
        for (WorkflowInspectionHearEntity workflowInspectionHearEntity : records) {
            String relevance = workflowInspectionHearEntity.getRelevance();
            String stateCode = workflowInspectionHearEntity.getStateCode();
            String stateName = workflowInspectionHearEntity.getStateName();
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, workflowInspectionHearEntity);
            workflowInspectionHearEntity.setStateCode(stateCode);
            workflowInspectionHearEntity.setStateName(stateName);
        }
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "调整检查科室-列表")
    @LogAnnotation(title = "调整检查科室", action = "列表")
    @PostMapping("/listdebugtaxorg")
    @RequiresPermissions("workflow:inspect:listdebugtaxorg")
    @ResponseBody
    @DS("oracle")
    public DataResult listdebugtaxorg(@RequestBody JcAjtzztbEntity jcAjtzztbEntity) throws ParseException {
        IPage<JcAjtzztbEntity> iPage = jcAjtzztbService.pageInfo(jcAjtzztbEntity);
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "案件调整")
    @LogAnnotation(title = "补偿案件管理", action = "保存补偿案件")
    @PostMapping("/saveBcajgl")
    @RequiresPermissions("workflow:inspect:listdebugtaxorg")
    @ResponseBody
    @DS("oracle")
    public DataResult saveBcajgl(@RequestBody JcAjtzztbEntity jcAjtzztbEntity) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        DmGySwjgEntity dmGySwjgEntity = dmGySwjgService.getSwjgDmBySwjgjc(jcAjtzztbEntity.getJsks());
        jcAjtzztbEntity.setJcajxxuuid(uuid);
        jcAjtzztbEntity.setLx("7");
        jcAjtzztbEntity.setJsksDm(dmGySwjgEntity.getSwjgDm());
        Boolean result = jcAjtzztbService.save(jcAjtzztbEntity);
        return DataResult.success(result);
    }

    @ApiOperation(value = "案件调整")
    @LogAnnotation(title = "自查检查人员调整", action = "未调整自查人员查询-不分页")
    @PostMapping("/queryzcAyxx")
    @ResponseBody
    public DataResult queryzcAyxx(@RequestBody JcAyxxEntity jcAyxxEntity) throws ParseException {
        return DataResult.success(jcAyxxService.queryzcAyxx(jcAyxxEntity));
    }

    @ApiOperation(value = "案件调整")
    @LogAnnotation(title = "自查检查人员调整", action = "未调整自查人员查询-分页")
    @PostMapping("/queryPagezcAyxx")
    @ResponseBody
    public DataResult queryPagezcAyxx(@RequestBody JcAyxxEntity jcAyxxEntity) throws ParseException {
        IPage<JcAyxxEntity> iPage = jcAyxxService.queryPagezcAyxx(jcAyxxEntity);
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "案件调整")
    @LogAnnotation(title = "自查检查人员调整", action = "未调整自查人员查询-分页")
    @PostMapping("/queryZcayxx")
    @ResponseBody
    public DataResult queryZcayxx(@RequestBody JcAjtzZcajxxEntity jcAjtzZcajxxEntity) throws ParseException {
        IPage<JcAjtzZcajxxEntity> iPage = jcAjtzZcajxxService.queryZcayxx(jcAjtzZcajxxEntity);
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "案件调整")
    @LogAnnotation(title = "自查检查人员调整", action = "未调整自查人员查询-不分页")
    @PostMapping("/exportZcayxx")
    @ResponseBody
    public DataResult exportZcayxx(@RequestBody JcAjtzZcajxxEntity jcAjtzZcajxxEntity) throws ParseException {
        List<JcAjtzZcajxxEntity> jcAjtzZcajxxEntityList = jcAjtzZcajxxService.exportZcayxx(jcAjtzZcajxxEntity);
        return DataResult.success(jcAjtzZcajxxEntityList);
    }

    @ApiOperation(value = "案件调整")
    @LogAnnotation(title = "审理员调整", action = "未调整审理人员查询-不分页")
    @PostMapping("/exportSly")
    @ResponseBody
    public DataResult exportSly(@RequestBody JcAjztSlytzEntity jcAjztSlytzEntity) throws ParseException {
        List<JcAjztSlytzEntity> jcAjztSlytzEntityList = jcAjtzSlytzService.queryAjtzSlytzList(jcAjztSlytzEntity);
        return DataResult.success(jcAjztSlytzEntityList);
    }

    @ApiOperation(value = "案源科室人员调整-列表(已调整)")
    @LogAnnotation(title = "案源科室人员调整", action = "列表(已调整)")
    @PostMapping("/listAykstzytz")
    @ResponseBody
    public DataResult listAykstzytz(@RequestBody JcAyxxDebugJgryEntity jcAyxxDebugJgryEntity) {
        IPage<JcAyxxDebugJgryEntity> iPage = jcAyxxService.pageInfoDeno(jcAyxxDebugJgryEntity);
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "案源科室人员调整-调整")
    @LogAnnotation(title = "案源科室人员调整", action = "调整")
    @PostMapping("/doTzAyksry")
    @RequiresPermissions("workflow:inspect:debugtaxorg")
    @ResponseBody
    @DS("oracle")
    public DataResult doTzAyksry(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {
        String currentUserId = httpSessionService.getCurrentUserId();
        String debugtaxorg = workflowInspectionHear.getDebugtaxorg();
        String content = workflowInspectionHear.getContent();
        String jcajxxuuid = workflowInspectionHear.getJcajxxuuid();
        //jcAyxxDebugJgryService.saveByDebugtaxorg(currentUserId, debugtaxorg, content, jcajxxuuid);
        return DataResult.success();
    }

    @ApiOperation(value = "调整检查科室-列表(已调整)")
    @LogAnnotation(title = "调整检查科室", action = "列表(已调整)")
    @PostMapping("/listdenodebugtaxorg")
    @RequiresPermissions("workflow:inspect:listdebugtaxorg")
    @ResponseBody
    @DS("oracle")
    public DataResult listdenodebugtaxorg(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear) {
        IPage<JcAyxxDebugEntity> iPage = jcAyxxService.pageInfoDeno(workflowInspectionHear);
        return DataResult.success(iPage);

    }

    @ApiOperation(value = "调整检查科室-调整")
    @LogAnnotation(title = "调整检查科室", action = "调整")
    @PostMapping("/debugtaxorg")
    @RequiresPermissions("workflow:inspect:debugtaxorg")
    @ResponseBody
    @DS("oracle")
    public DataResult debugtaxorg(@RequestBody JcAjtzztbEntity jcAjtzztbEntity) {
        String selectedValue = jcAjtzztbEntity.getSelectedValue();
        String[] parts = selectedValue.split("\\|"); // 注意：需要对竖线 | 转义
        String swjgDm = parts[0]; // 获取 "12101009150"
        String swjgjc = parts[1]; // 获取 "沈阳市税务局第一稽查局案件审理科"
        jcAjtzztbEntity.setTzhksDm(swjgDm);
        jcAjtzztbEntity.setTzhks(swjgjc);
        jcAjtzztbEntity.setLx(Constants.AJZT_LX_YTZ);
        jcAjtzztbService.updateById(jcAjtzztbEntity);
        return DataResult.success();
    }

    @ApiOperation(value = "调整检查科室")
    @LogAnnotation(title = "自查案件调整", action = "保存案件调整科室信息")
    @PostMapping("/saveZcajks")
    @ResponseBody
    @DS("oracle")
    public DataResult saveZcajks(@RequestBody JcAjtzZcajxxEntity jcAjtzZcajxxEntity) {
        String selectedValue = jcAjtzZcajxxEntity.getSelectedValue();
        String[] parts = selectedValue.split("\\|"); // 注意：需要对竖线 | 转义
        String swjgDm = parts[0]; // 获取 "12101009150"
        String swjgjc = parts[1]; // 获取 "沈阳市税务局第一稽查局案件审理科"
        jcAjtzZcajxxEntity.setTzhksDm(swjgDm);
        jcAjtzZcajxxEntity.setTzhks(swjgjc);
        jcAjtzZcajxxEntity.setZcajlx(Constants.AJZT_LX_YTZ);
        jcAjtzZcajxxService.updateById(jcAjtzZcajxxEntity);
        return DataResult.success();
    }

    @PostMapping("/exportData")
    @ResponseBody
    public DataResult exportData(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear)
            throws java.io.IOException {

        List<JcAyxxEntity> jcAyxxEntityList = jcAyxxService.list(workflowInspectionHear);

        if (jcAyxxEntityList.size() > 0) {

            List<JcAyxxExcel> jcAyxxExcelList = new ArrayList<>();
            for (JcAyxxEntity jcAyxx : jcAyxxEntityList) {

                JcAyxxExcel jcAyxxExcel = JcAyxxExcel.builder().jcajxxuuid(jcAyxx.getJcajxxuuid())
                        .jcajbh(jcAyxx.getJcajbh()).ajmc(jcAyxx.getAjmc()).larq(jcAyxx.getLarq())
                        .nsrsbh(jcAyxx.getNsrsbh()).nsrmc(jcAyxx.getNsrmc()).jcry(jcAyxx.getJcry())
                        .jcxm(jcAyxx.getJcxm()).taxorgcode("").cause("").build();

                jcAyxxExcelList.add(jcAyxxExcel);

            }

            String fileName = TsBqUtils.getUUID() + ".xlsx";

            String excelPath = filePath + fileName;
            OutputStream out = null;

            try {
                out = new FileOutputStream(excelPath);
                ExcelWriter writer = EasyExcelFactory.getWriter(out);

                Sheet sheet1 = new Sheet(1, 0, JcAyxxExcel.class);
                sheet1.setSheetName("数据");
                writer.write(jcAyxxExcelList, sheet1);
                // 将上下文中的最终 outputStream 写入到指定文件中
                writer.finish();
                File excelFile = new File(excelPath);
                List<Object> excelFileInfo = new ArrayList<>();
                excelFileInfo.add(fileName);
                excelFileInfo.add(excelFile.length());
                excelFileInfo.add(fileUrl + "/" + fileName);

                return DataResult.success(excelFileInfo);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return DataResult.fail("xlsx生成失败：" + e.toString());
            } finally {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return DataResult.fail("xlsx生成失败：" + e.toString());
                }

            }

        } else {
            return DataResult.fail("传入的ids为null！");
        }
    }

    @PostMapping("/exportAyxxJgryData")
    @ResponseBody
    @DS("oracle")
    public DataResult exportAyxxJgryData(@RequestBody JcAyxxDebugJgryEntity jcAyxxDebugJgryEntity) throws ParseException {
        List<JcAyxxEntity> jcAyxxEntityList = jcAyxxService.getAyxxJgry(jcAyxxDebugJgryEntity);
        return DataResult.success(jcAyxxEntityList);
    }

//    @PostMapping("/exportAyxxJgryData")
//    @ResponseBody
//    @DS("oracle")
//    public DataResult exportAyxxJgryData(@RequestBody WorkflowInspectionHearEntity workflowInspectionHear)
//            throws java.io.IOException, ParseException {
//        List<JcAyxxEntity> jcAyxxEntityList = jcAyxxService.listAyxxJgry(workflowInspectionHear);
//        if (jcAyxxEntityList.size() > 0) {
//            List<JcAyxxJgRyExcel> jcAyxxExcelList = new ArrayList<>();
//            for (JcAyxxEntity jcAyxx : jcAyxxEntityList) {
//                JcAyxxJgRyExcel jcAyxxJgRyExcel = JcAyxxJgRyExcel.builder().jcayxxuuid(jcAyxx.getJcayxxuuid())
//                        .jcaybh(jcAyxx.getJcaybh()).nsrsbh(jcAyxx.getNsrsbh())
//                        .nsrmc(jcAyxx.getNsrmc()).lrrq(jcAyxx.getLrrq())
//                        .taxorgcode("").cause("").zcryDm("").xcryDm("").build();
//                jcAyxxExcelList.add(jcAyxxJgRyExcel);
//            }
//            String fileName = TsBqUtils.getUUID() + ".xlsx";
//            String excelPath = filePath + fileName;
//            OutputStream out = null;
//            try {
//                out = new FileOutputStream(excelPath);
//                ExcelWriter writer = EasyExcelFactory.getWriter(out);
//
//                Sheet sheet1 = new Sheet(1, 0, JcAyxxJgRyExcel.class);
//                sheet1.setSheetName("数据");
//                writer.write(jcAyxxExcelList, sheet1);
//                // 将上下文中的最终 outputStream 写入到指定文件中
//                writer.finish();
//                File excelFile = new File(excelPath);
//                List<Object> excelFileInfo = new ArrayList<>();
//                excelFileInfo.add(fileName);
//                excelFileInfo.add(excelFile.length());
//                excelFileInfo.add(fileUrl + "/" + fileName);
//                return DataResult.success(excelFileInfo);
//
//            } catch (FileNotFoundException e) {
//                e.printStackTrace();
//                return DataResult.fail("xlsx生成失败：" + e.toString());
//            } finally {
//                try {
//                    out.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                    return DataResult.fail("xlsx生成失败：" + e.toString());
//                }
//            }
//        } else {
//            return DataResult.fail("没有待导出的数据！");
//        }
//    }

    @PostMapping("/importDataAyxxJgryData")
    @ResponseBody
    public DataResult importDataAyxxJgryData(@RequestParam("file") MultipartFile[] files) throws java.io.IOException {

        if (files != null && files.length > 0) {
            for (MultipartFile file : files) {
                InputStream inputStream = null;
                try {
                    inputStream = file.getInputStream();
                    Sheet sheet = new Sheet(1, 1);
                    List<Object> readList = EasyExcelFactory.read(inputStream, sheet);
                    List<Map<String, Object>> listMap = new ArrayList<>();

                    for (Object object : readList) {
                        List<String> list = TsBqUtils.castList(object, String.class);
                        String jcayxxuuid = list.get(0);//
                        String taxcodeorg = list.get(6); //
                        String cause = list.get(5); //
                        String zcry = list.get(7); //
                        String xcry = list.get(8); //
                        String jcaybh = list.get(1); //
                        if (TsBqUtils.isNull(jcayxxuuid)) {
                            return DataResult.fail("表格中存在【稽查案源信息UUID】为空的！");
                        }

                        if (TsBqUtils.isNull(jcaybh)) {
                            return DataResult.fail("表格中存在【稽查案源编号】为空的！");
                        }
                        if (TsBqUtils.isNull(taxcodeorg)) {
                            return DataResult.fail("表格中存在【调整后的税务机关代码】为空的！");
                        }
                        if (TsBqUtils.isNull(cause)) {
                            return DataResult.fail("表格中存在【调整原因】为空的！");
                        }
                        if (TsBqUtils.isNull(zcry)) {
                            return DataResult.fail("表格中存在【调整后的主查税务人员代码】为空的！");
                        }
                        if (TsBqUtils.isNull(xcry)) {
                            return DataResult.fail("表格中存在【调整后的协查税务人员代码(多个以,分割)】为空的！");
                        }

                        JcAyxxEntity jcAyxxEntity = jcAyxxService.getByJcayxxuuid(jcayxxuuid);
                        if (TsBqUtils.isNull(jcAyxxEntity)) {
                            return DataResult.fail("表格中存在【稽查案源信息UUID】不合法：值为" + jcayxxuuid);
                        }
                        DmGySwjgEntity swjgDm = dmGySwjgService.getSwjgDm(taxcodeorg);
                        if (TsBqUtils.isNull(swjgDm)) {
                            return DataResult.fail("表格中存在【调整后的税务机关代码】不合法：值为" + taxcodeorg);
                        }
                        Map<String, Object> map = new HashMap<>();
                        map.put("swjgDm", taxcodeorg);
                        map.put("content", cause);
                        map.put("jcayxxuuid", jcayxxuuid);
                        map.put("jczcryDm", zcry);
                        map.put("jcaybh", jcaybh);
                        map.put("jcxcryDm", xcry);
                        listMap.add(map);
                    }
                    String currentUserId = httpSessionService.getCurrentUserId();
                    if (TsBqUtils.isNotNull(listMap)) {
                        for (Map<String, Object> map : listMap) {
                            String swjgDm = map.get("swjgDm").toString();
                            String content = map.get("content").toString();
                            String jcayxxuuid = map.get("jcayxxuuid").toString();
                            String jczcryDm = map.get("jczcryDm").toString();
                            String jcxcryDm = map.get("jcxcryDm").toString();
                            String jcaybh = map.get("jcaybh").toString();
                            jcAyxxDebugJgryService.saveByDebugtaxorg(currentUserId, swjgDm, content, jcayxxuuid, jczcryDm, jcxcryDm, jcaybh);
                        }
                    }
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                } finally {
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                            return DataResult.fail(e.getMessage());
                        }
                    }
                }
            }
        }
        return DataResult.success();

    }

    @PostMapping("/importData")
    @ResponseBody
    public DataResult importData(@RequestParam("file") MultipartFile[] files) throws java.io.IOException {

        if (files != null && files.length > 0) {
            for (MultipartFile file : files) {
                InputStream inputStream = null;
                try {
                    inputStream = file.getInputStream();
                    Sheet sheet = new Sheet(1, 1);
                    List<Object> readList = EasyExcelFactory.read(inputStream, sheet);
                    List<Map<String, Object>> listMap = new ArrayList<>();

                    for (Object object : readList) {
                        List<String> list = TsBqUtils.castList(object, String.class);

                        String jcuuid = list.get(0);//

                        String taxcodeorg = list.get(8); //
                        String cause = list.get(9); //

                        if (TsBqUtils.isNull(jcuuid)) {
                            return DataResult.fail("表格中存在【检查UUID】为空的！");
                        }

                        if (TsBqUtils.isNull(taxcodeorg)) {
                            return DataResult.fail("表格中存在【调整后的税务机关代码】为空的！");
                        }

                        if (TsBqUtils.isNull(cause)) {
                            return DataResult.fail("表格中存在【调整原因】为空的！");
                        }

                        JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(jcuuid);
                        if (TsBqUtils.isNull(jcAjxxEntity)) {
                            return DataResult.fail("表格中存在【检查UUID】不合法：值为" + jcuuid);
                        }

                        DmGySwjgEntity swjgDm = dmGySwjgService.getSwjgDm(taxcodeorg);
                        if (TsBqUtils.isNull(swjgDm)) {
                            return DataResult.fail("表格中存在【调整后的税务机关代码】不合法：值为" + taxcodeorg);
                        }

                        Map<String, Object> map = new HashMap<>();

                        map.put("debugtaxorg", taxcodeorg);
                        map.put("content", cause);
                        map.put("jcajxxuuid", jcuuid);
                        listMap.add(map);

                    }
                    String currentUserId = httpSessionService.getCurrentUserId();
                    if (TsBqUtils.isNotNull(listMap)) {
                        for (Map<String, Object> map : listMap) {

                            String debugtaxorg = map.get("debugtaxorg").toString();
                            String content = map.get("content").toString();
                            String jcajxxuuid = map.get("jcajxxuuid").toString();
                            jcAyxxDebugService.saveByDebugtaxorg(currentUserId, debugtaxorg, content, jcajxxuuid);

                        }

                    }

                } catch (Exception e) {
                    System.out.println(e.getMessage());

                } finally {
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                            return DataResult.fail(e.getMessage());
                        }
                    }
                }
            }
        }
        return DataResult.success();
    }

    @PostMapping("/exportTaxorgcode")
    @ResponseBody
    @DS("oracle")
    public DataResult exportTaxorgcode() {
        List<DmGySwjgEntity> dmGySwjglist = dmGySwjgService.getDmGyswjgByCurrentMan();
        return DataResult.success(dmGySwjglist);
    }
}
